package com.ruiysoft.service.order.impl;

import com.ruiysoft.conf.mq.OrderMqConfig;
import com.ruiysoft.service.order.IOrderMonitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;


@Component
public class OrderMonitorImpl implements IOrderMonitor {
	Logger logger = LoggerFactory.getLogger(OrderMonitorImpl.class);

	@Autowired
	RabbitTemplate order;

	@Override
	public void commitMonitor(String orderNo) {
//            JSON.toJSONString(new OrderMonitorMessage(orderNo, MonitorType.COMMIT)).getBytes(StandardCharsets.UTF_8);
		order.convertAndSend(
				OrderMqConfig.delay30MinuteExchangeName,
				OrderMqConfig.delay30MinuteRouteKey,
				new OrderMonitorMessage(orderNo, MonitorType.COMMIT),
				message -> {
					message.getMessageProperties().setExpiration(OrderMqConfig.delay30MinuteDelayTime);
					return message;
				}
		);
		if (logger.isInfoEnabled()) {
			logger.info("订单提交后30分钟支付监控信息已经提交,OrderNo=" + orderNo);
		}
	}

	/**
	 * 还没实现任何代码
	 *
	 * @param orderNo
	 */
	@Deprecated
	@Override
	public void paidMonitor(String orderNo) {

	}

	/**
	 * 还没实现任何代码
	 *
	 * @param orderNo
	 */
	@Override
	public void cancelMonitor(String orderNo) {

	}


	public enum MonitorType {
		COMMIT, PAID, CANCEL
	}

	public static class OrderMonitorMessage implements Serializable {
		private String orderNo;
		private MonitorType monitorType;

		public OrderMonitorMessage() {
		}

		public OrderMonitorMessage(String orderNo, MonitorType monitorType) {
			this.orderNo = orderNo;
			this.monitorType = monitorType;
		}

		public String getOrderNo() {
			return orderNo;
		}

		public OrderMonitorMessage setOrderNo(String orderNo) {
			this.orderNo = orderNo;
			return this;
		}

		public MonitorType getMonitorType() {
			return monitorType;
		}

		public OrderMonitorMessage setMonitorType(MonitorType monitorType) {
			this.monitorType = monitorType;
			return this;
		}
	}
}
